package com.rico.kok.controller.post;

import com.alibaba.fastjson.JSONObject;
import com.rico.kok.entity.Label;
import com.rico.kok.entity.Post;
import com.rico.kok.entity.User;
import com.rico.kok.service.interf.LabelService;
import com.rico.kok.service.interf.PostService;
import com.rico.kok.service.editor.UploadImageService;
import com.rico.kok.tool.MessageUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequiresRoles("user")
public class SendPostController {

    @Autowired
    UploadImageService uploadImageService;

    @Autowired
    PostService postService;

    @Autowired
    MessageUtils messageUtils;

    @Autowired
    LabelService labelService;

    //跳转到发帖页面
    @RequestMapping("/sendPost.html")
    public String toSendPost(Model model) {
        List<Label> labelList = labelService.selectAllLabel();
        model.addAttribute("labels", labelList);
        return "post/send_post";
    }

    //上传图片
    @PostMapping("/uploadImg.htm")
    @ResponseBody
    public JSONObject uploadImg(MultipartFile file) {
        JSONObject json = new JSONObject();
        try {
            json = uploadImageService.uploadImg(file);
        } catch (Exception e) {
            System.out.println("上传编辑器图片失败：" + e);
        }
        return json;
    }

    //发布
    @PostMapping("/sendPost.htm")
    public String sendPost(@RequestParam("title") String title, @RequestParam("detail") String detail, @RequestParam("content") String content, @RequestParam("moduleId") String mld, Model model, HttpSession session, HttpServletResponse response) {
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("postConfig.properties"));
        } catch (IOException e) {
            System.out.println("内容配置文件加载失败！");
            e.printStackTrace();
            messageUtils.setMsg(model, "错误", "服务器出现了错误", "error");
            return "msg";
        }
        if (title.length() < Integer.parseInt(properties.getProperty("post.title.length.min")) || title.length() > Integer.parseInt(properties.getProperty("post.title.length.max"))) {
            messageUtils.setMsg(model, "错误", "文章的标题长度不合理！", "error");
            return "msg";
        }
        if (detail.length() < Integer.parseInt(properties.getProperty("post.detail.length.min")) || detail.length() > Integer.parseInt(properties.getProperty("post.detail.length.max"))) {
            messageUtils.setMsg(model, "文章的描述长度不合理", "error");
            return "msg";
        }
        if (content.length() < Integer.parseInt(properties.getProperty("post.content.length.min")) || content.length() > Integer.parseInt(properties.getProperty("post.content.length.max"))) {
            messageUtils.setMsg(model, "文章的内容长度不合理", "error");
            return "msg";
        }
        //新建post对象
        Post post = new Post();
        //获取当前用户信息
        User user = (User) session.getAttribute("user");
        //放入用户id
        post.setUserId(user.getUserId());
        //放入标题
        post.setPostTitle(title);
        //放入描述
        post.setPostDetail(detail);
        //放入内容
        post.setPostContent(content);
        //创建标签集合
        String[] labels = mld.split(",");
        List list = Arrays.asList(labels);
        //调用service
        postService.insertPost(post,list);
        //回到主界面
        //跳转到iframe的父页面
        /*PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.print("<script>window.parent.location.href='/';</script>");
        out.flush();*/
        return "redirect:/";
    }
}